package com.cnzhizi.wheelpop.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cnzhizi.wheelpop.R;
import com.cnzhizi.wheelpop.model.OneModel;
import com.cnzhizi.wheelpop.model.ThreeModel;
import com.cnzhizi.wheelpop.model.TwoModel;

import java.util.ArrayList;
import java.util.List;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ListWheelAdapter;

/**
 * Created by Sun on 2016/7/15.
 */
public class SkmModiPopup extends PopupWindow implements OnWheelChangedListener {
    private WheelView mWheelOne;
    private WheelView mWheelTwo;
    private WheelView mWheelThree;
    private Button mBtnConfirm;
    private Button mBtnCancel;
    private TextView tv_title;

    private String title="请选择";
    private int level=3;

    private Context context;
    private SkmPopOnClickListener onClickListener;
    /**
     * 所有一级  内含所有数据
     */
    List<OneModel> oneModelList = new ArrayList<OneModel>();

    private View rootView;

    /**
     *
     * @param context
     * @param onClickListener
     * @param oneModelList 所有数据
     * @param title 标题
     * @param level 默认3
     */
    static View onclickView;//外部点击的
    public SkmModiPopup(Context context, SkmPopOnClickListener onClickListener,List<OneModel> oneModelList,String title,int level,View onclickView){
        super(context);
        this.context=context;
        this.onClickListener=onClickListener;
        this.oneModelList=oneModelList;
        this.title=title;
        this.level=level;
        SkmModiPopup.onclickView =onclickView;

             pOneCurrent =0;
             pTwoCurrent = 0;
             pThreeCurrent =0;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = inflater.inflate(R.layout.activity_main_three, null);

        initViews();
        initListener();
        initData();


        // 设置SelectPicPopupWindow的View
        this.setContentView(rootView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopupAnimation);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x80000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        rootView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            @SuppressLint("ClickableViewAccessibility")
            public boolean onTouch(View v, MotionEvent event) {

                int height = rootView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

    }

    private void initViews() {
        mWheelOne = (WheelView) rootView.findViewById(R.id.id_one);
        mWheelTwo = (WheelView) rootView.findViewById(R.id.id_two);
        mWheelThree = (WheelView) rootView.findViewById(R.id.id_three);
        mBtnConfirm = (Button) rootView.findViewById(R.id.btn_confirm);
        mBtnCancel = (Button) rootView.findViewById(R.id.btn_cancel);
        tv_title = (TextView) rootView.findViewById(R.id.tv_title);
        if(level==1){
            mWheelTwo.setVisibility(View.GONE);
            mWheelThree.setVisibility(View.GONE);
        }else if(level==2){
            mWheelThree.setVisibility(View.GONE);
        }
    }
    private void initData() {
        tv_title.setText(title);

        //处理空值问题
        for(int i=0;i<oneModelList.size();i++){
            List<TwoModel> twoModelListTemp = oneModelList.get(i).getTwoModelList();//获取第二级别
            if (twoModelListTemp == null) {//二级级为空的默认值
                twoModelListTemp = new ArrayList<TwoModel>();
                TwoModel twoModelTemp = new TwoModel();
                twoModelTemp.setName("");
                twoModelTemp.setId("8888");
                twoModelListTemp.add(twoModelTemp);
                //将此数据统一加到大数据集合中
                oneModelList.get(i).setTwoModelList(twoModelListTemp);
            }
            for (int j=0;j<twoModelListTemp.size();j++){
                List<ThreeModel> threeModelListTemp = oneModelList.get(i).getTwoModelList().get(j).getThreeModelList();//获取第三级别

                if (threeModelListTemp == null) {//三级为空的默认值
                    threeModelListTemp = new ArrayList<ThreeModel>();
                    ThreeModel threeModelTemp = new ThreeModel();
                    threeModelTemp.setId("9999");
                    threeModelTemp.setName("");
                    threeModelListTemp.add(threeModelTemp);
                    //将此数据统一加到大数据集合中
                    oneModelList.get(i).getTwoModelList().get(j).setThreeModelList(threeModelListTemp);
                }
            }


        }


        mWheelOne.setViewAdapter(new ListWheelAdapter<String>(context, oneModelList));
        // 设置可见条目数量
        mWheelOne.setVisibleItems(7);
        mWheelTwo.setVisibleItems(7);
        mWheelThree.setVisibleItems(7);
        updateTwoWheelData();
        updateThreeWheelData();
    }
    private void initListener() {
        // 添加change事件
        mWheelOne.addChangingListener(this);
        // 添加change事件
        mWheelTwo.addChangingListener(this);
        // 添加change事件
        mWheelThree.addChangingListener(this);
        // 添加onclick事件
        mBtnConfirm.setOnClickListener(onClickListener);
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SkmModiPopup.this.dismiss();
            }
        });

    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mWheelOne) {
            updateTwoWheelData();
        } else if (wheel == mWheelTwo) {
            updateThreeWheelData();
        } else if (wheel == mWheelThree) {

        }
        getSelectedData();
    }


    /**
     * 获取选中的数据的值和信息
     */
   static int pOneCurrent =0;
    static  int pTwoCurrent = 0;
    static int pThreeCurrent =0;
    private void getSelectedData() {
          pOneCurrent = mWheelOne.getCurrentItem();
          pTwoCurrent = mWheelTwo.getCurrentItem();
          pThreeCurrent = mWheelThree.getCurrentItem();
        Log.d("skm",pOneCurrent+" "+pTwoCurrent+"  "+pThreeCurrent);

//
    }

    /**
     * 根据当前的二级，更新三级WheelView的信息
     */
    private void updateThreeWheelData() {
        int pCurrent = mWheelOne.getCurrentItem();
        int pTwoCurrent = mWheelTwo.getCurrentItem();

        List<ThreeModel> threeModelList = oneModelList.get(pCurrent).getTwoModelList().get(pTwoCurrent).getThreeModelList();//获取第三级别

        /*if (threeModelList == null) {//三级为空的默认值
            threeModelList = new ArrayList<ThreeModel>();
            ThreeModel threeModel = new ThreeModel();
            threeModel.setId("9999");
            threeModel.setName("");
            threeModelList.add(threeModel);
            //将此数据统一加到大数据集合中
            oneModelList.get(pCurrent).getTwoModelList().get(pTwoCurrent).setThreeModelList(threeModelList);
        }*/
        mWheelThree.setViewAdapter(new ListWheelAdapter<String>(context, threeModelList));
        mWheelThree.setCurrentItem(0);

    }

    /**
     * 根据当前的一级，更新二级WheelView的信息
     */
    private void updateTwoWheelData() {
        int pCurrent = mWheelOne.getCurrentItem();

        List<TwoModel> twoModelList = oneModelList.get(pCurrent).getTwoModelList();//获取第二级别
    /*    if (twoModelList == null) {//二级级为空的默认值
            twoModelList = new ArrayList<TwoModel>();
            TwoModel twoModel = new TwoModel();
            twoModel.setName("");
            twoModel.setId("8888");
            twoModelList.add(twoModel);
            //将此数据统一加到大数据集合中
            oneModelList.get(pCurrent).setTwoModelList(twoModelList);
        }*/
        mWheelTwo.setViewAdapter(new ListWheelAdapter<String>(context, twoModelList));
        mWheelTwo.setCurrentItem(0);
        updateThreeWheelData();
    }

    public static    abstract class SkmPopOnClickListener implements View.OnClickListener{
        public SkmPopOnClickListener() {

        }

        @Override
        public void onClick(View v) {
            myOnClick(pOneCurrent,pTwoCurrent,pThreeCurrent, v,onclickView);
        }
        public abstract void myOnClick(int one,int two,int three, View v,View root);

    }

}
